package com.huaxiafinance.management.controller;

import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.huaxiafinance.management.dto.request.QueryRoleDto;
import com.huaxiafinance.management.model.Role;
import com.huaxiafinance.management.service.RoleService;
import com.huaxiafinance.management.util.MessageResult;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.List;
import java.util.Map;

@Controller("roleController")
@RequestMapping("/role")
public class RoleController {

  private static final Logger log = Logger.getLogger(RoleController.class);

  @Autowired
  private RoleService roleService;

  @RequestMapping(value = "/addRole", produces = "application/json; charset=utf-8")
  public @ResponseBody
  String addRole(HttpServletRequest request) throws Exception {
    String json = request.getParameter("json");

    log.info("新增角色请求参数" + json);

    MessageResult messageResult = new MessageResult();

    ObjectMapper mapper = new ObjectMapper();
    mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
    Role role = mapper.readValue(json, Role.class);

    boolean flag = roleService.addRole(role);

    if (flag) {
      messageResult.setSuccessResult();
    } else {
      messageResult.setFailureResult();
    }

    log.info("新增角色返回参数" + messageResult.getAsJSON());

    return messageResult.getAsJSON();
  }

  @RequestMapping(value = "/queryRole", produces = "application/json; charset=utf-8")
  public @ResponseBody
  String queryRole(@Valid @ModelAttribute("dto") QueryRoleDto dto) throws Exception {
    MessageResult messageResult = new MessageResult();

    List<Role> list = roleService.queryRole(dto);
    messageResult.setData(list);
    messageResult.setRows(roleService.countRole(dto));
    messageResult.setSuccessResult();

    return messageResult.getAsJSON();
  }

  @SuppressWarnings("unchecked")
  @RequestMapping(value = "/updateRole", produces = "application/json; charset=utf-8")
  public @ResponseBody
  String updateRole(HttpServletRequest request) throws Exception {
    String json = request.getParameter("json");

    log.info("修改角色请求参数" + json);

    MessageResult messageResult = new MessageResult();

    ObjectMapper mapper = new ObjectMapper();
    mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
    Role role = mapper.readValue(json, Role.class);
    Map<String, Object> map = mapper.readValue(json, Map.class);
    role.setId(Integer.valueOf(map.get("roleId").toString()));

    boolean flag = roleService.updateRole(role);

    if (flag) {
      messageResult.setSuccessResult();
    } else {
      messageResult.setFailureResult();
    }

    log.info("修改角色返回参数" + messageResult.getAsJSON());

    return messageResult.getAsJSON();
  }

  @RequestMapping(value = "/delRole", produces = "application/json; charset=utf-8")
  public @ResponseBody
  String delRole(Integer id) throws Exception {

    log.info("删除角色请求参数" + id);

    MessageResult messageResult = null;

    messageResult = roleService.delRole(id);

    log.info("删除角色返回参数" + messageResult.getAsJSON());

    return messageResult.getAsJSON();
  }
}